
package SystemDataTypes;

import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Administrador
 */
public class Airport {

    public String id;
    public Vector3 position;   // position   
        
    public Airport(String id, Vector3 position) {
        this.id = id;
        this.position = position;
    }
       
    public Airport(JSONObject jSONObject) {        
        try {

            if (jSONObject.has("id"))
                this.id = jSONObject.getString("id");
            
            if (jSONObject.has("position")){
                JSONArray array = jSONObject.getJSONArray("position");
                this.position = new Vector3();
                this.position.x = array.getDouble(0);
                this.position.y = array.getDouble(1);
                this.position.z = array.getDouble(2);
            }                                                       
        } catch (JSONException ex) {
            Logger.getLogger(FlightData.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
    
  public Airport(ResultSet rs) {
        try {
            this.id = rs.getString("id");
            String posStr = rs.getString("Position");
            JSONArray array = new JSONArray(posStr);
            this.position = new Vector3(array.getDouble(0),array.getDouble(1),array.getDouble(2));            
        } catch (Exception ex) {
            Logger.getLogger(FlightData.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  
  @Override
  public String toString(){
//        JSONObject container = new JSONObject();       
        JSONObject data = new JSONObject();              

        try {
            data.put("id", this.id);
            JSONArray jposition = new JSONArray();        
            jposition.put(this.position.x);
            jposition.put(this.position.y);
            jposition.put(this.position.z);
            data.put("position", jposition);
         //   container.put("FlightData", data);
            
        } catch (JSONException ex) {
            Logger.getLogger(FlightData.class.getName()).log(Level.SEVERE, null, ex);
        }
        
  return data.toString();
  }
  
}
